package cn.lingyangwl.agile.model;

import cn.hutool.core.date.DateUtil;
import cn.lingyangwl.agile.model.enums.BoolEnum;
import cn.lingyangwl.framework.tool.core.StringUtils;
import com.alibaba.fastjson2.JSON;
import org.mapstruct.Named;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author shenguangyang
 */
public interface BaseAssembly {
    String BOOL_TO_STR = "boolToStr";
    String STR_TO_BOOL = "strToBool";
    String STR_TO_LIST = "strToList";
    String STR_TO_SET = "strToSet";
    String SET_TO_STR = "setToStr";
    String JSON_STR_TO_LIST = "jsonStrToList";
    String JSON_STR_TO_SET = "jsonStrToSet";
    String ARRAY_TO_STR = "arrayToStr";
    String ARRAY_TO_LIST = "arrayToList";
    String LIST_TO_STR = "listToStr";
    String LIST_TO_JSON_STR = "listToJsonStr";
    String SET_TO_JSON_STR = "setToJsonStr";
    String LONG_TO_DATE = "longToDate";

    @Named(JSON_STR_TO_LIST)
    default List<String> jsonStrToList(String value) {
        if (StringUtils.isEmpty(value)) {
            return Collections.emptyList();
        }
        return JSON.parseArray(value, String.class);
    }

    @Named(JSON_STR_TO_SET)
    default Set<String> jsonStrToSet(String value) {
        if (StringUtils.isEmpty(value)) {
            return Collections.emptySet();
        }
        return new HashSet<>(JSON.parseArray(value, String.class));
    }

    @Named(BOOL_TO_STR)
    default String boolToStr(Boolean value) {
        return BoolEnum.of(value).getCode();
    }

    @Named(STR_TO_BOOL)
    default Boolean strToBool(String value) {
        return BoolEnum.of(value) == BoolEnum.TRUE;
    }

    @Named(STR_TO_LIST)
    default List<String> strToList(String value) {
        if (StringUtils.isEmpty(value)) {
            return Collections.emptyList();
        }
        return StringUtils.toList(value, ",", String.class);
    }

    @Named(STR_TO_SET)
    default Set<String> strToSet(String value) {
        if (StringUtils.isEmpty(value)) {
            return Collections.emptySet();
        }
        return new HashSet<>(StringUtils.toList(value, ",", String.class));
    }

    @Named(ARRAY_TO_STR)
    default String arrayToStr(Long[] value) {
        return Arrays.stream(value).map(String::valueOf).collect(Collectors.joining(","));
    }

    @Named(LIST_TO_STR)
    default String listToStr(List<String> value) {
        if (Objects.isNull(value)) {
            return null;
        }
        return String.join(",", value);
    }

    @Named(SET_TO_STR)
    default String setToStr(Set<String> value) {
        if (Objects.isNull(value)) {
            return null;
        }
        return String.join(",", value);
    }

    @Named(LIST_TO_JSON_STR)
    default String listToJsonStr(List<String> value) {
        return JSON.toJSONString(value);
    }

    @Named(SET_TO_JSON_STR)
    default String setToJsonStr(Set<String> value) {
        return JSON.toJSONString(value);
    }


    @Named(ARRAY_TO_LIST)
    default List<Long> arrayToList(Long[] value) {
        return Arrays.stream(value).collect(Collectors.toList());
    }

    @Named(LONG_TO_DATE)
    default Date longToDate(Long value) {
        return DateUtil.date(value).toJdkDate();
    }
}
